package org.yoao.crm.struts.action;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.actions.DispatchAction;
import org.yoao.crm.bean.StaticBean;
import org.yoao.crm.page.framework.AbstractViewPage;
import org.yoao.crm.page.framework.IViewPageService;
import org.yoao.crm.page.framework.ViewPageHelper;

/**
 * @author yxwonline.com
 * @description 
 * @date 2010-7-31 下午11:23:46
 */
public class BaseAction extends DispatchAction {
	
	public List getPage(HttpServletRequest request, AbstractViewPage viewPage, Map map) throws Exception{
		String pageAction = request.getParameter(ViewPageHelper.PAGE_ACTION);
		String pageIndexStr = request.getParameter(ViewPageHelper.PAGE_NO);
		Integer pageIndex = null;
		if(pageIndexStr != null) pageIndex = Integer.valueOf(pageIndexStr);
		
		//声明分页对象 
		IViewPageService viewPageService = 
			(IViewPageService)request.getSession().getAttribute(ViewPageHelper.SESSION_PAGE);
		
		//第一次请求 
		if(pageAction == null || viewPageService == null){
			viewPageService = viewPage; //构建一个新的分页对象 
			viewPageService.setPageSize(StaticBean.PAGESIZE_VALUE, map); //设置分页大小 
		}else{
			if(ViewPageHelper.SPECIAL_PAGE.equals(pageAction)){	
				if(pageIndex == null) pageIndex = new Integer(1); //如果页数为空，则默认为1 
				ViewPageHelper.doAction(viewPageService, pageAction, pageIndex.intValue());
			}else{
				ViewPageHelper.doAction(viewPageService, pageAction);
			}
		}
		
		List dataList = viewPageService.getPageData(map);
		
		request.getSession().setAttribute(ViewPageHelper.SESSION_PAGE, viewPageService);
		
		return dataList;
	}
}